import { get, post, put, del } from '../request';
import type { Member, FilterOptions } from '@/store/modules/member';

// 会员列表查询参数
export interface MemberListParams {
  page?: number;
  limit?: number;
  search?: string;
  status?: 'active' | 'inactive' | 'all';
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

// 会员列表响应数据
export interface MemberListResponse {
  members: Member[];
  pagination: {
    current: number;
    pageSize: number;
    total: number;
  };
}

// 创建会员参数
export interface CreateMemberParams {
  name: string;
  phone: string;
  email?: string;
  avatar?: string;
  notes?: string;
}

// 更新会员参数
export interface UpdateMemberParams extends Partial<CreateMemberParams> {
  id: string;
  status?: 'active' | 'inactive';
}

// 会员API
export const memberApi = {
  // 获取会员列表
  getMembers: (params: MemberListParams = {}) => {
    return get<MemberListResponse>('/members', params);
  },

  // 获取会员详情
  getMemberById: (id: string) => {
    return get<Member>(`/members/${id}`);
  },

  // 创建会员
  createMember: (params: CreateMemberParams) => {
    return post<Member>('/members', params);
  },

  // 更新会员
  updateMember: (params: UpdateMemberParams) => {
    const { id, ...data } = params;
    return put<Member>(`/members/${id}`, data);
  },

  // 删除会员
  deleteMember: (id: string) => {
    return del<{ message: string }>(`/members/${id}`);
  },

  // 批量删除会员
  batchDeleteMembers: (ids: string[]) => {
    return del<{ message: string }>('/members/batch', { ids });
  },

  // 更新会员状态
  updateMemberStatus: (id: string, status: 'active' | 'inactive') => {
    return put<Member>(`/members/${id}/status`, { status });
  },

  // 搜索会员
  searchMembers: (keyword: string, filters?: FilterOptions) => {
    return get<MemberListResponse>('/members/search', {
      keyword,
      ...filters,
    });
  },

  // 获取会员统计信息
  getMemberStats: () => {
    return get<{
      total: number;
      active: number;
      inactive: number;
      newThisMonth: number;
    }>('/members/stats');
  },
};